package forum;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;


import javax.swing.text.html.Option;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumCases  extends IninAndEnd{

//    /**
//     * 注册功能
//     */
//    @Order(1)
//    @Test
//    void sign_up(String username,String nickname,String password) {
//        webDriver.get("http://39.107.238.82:58080/sign-up.html");
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
//        //用户名
//        webDriver.findElement(By.cssSelector("#username")).sendKeys("注册测试");
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
//        //昵称
//        webDriver.findElement(By.cssSelector("#nickname")).sendKeys("小明");
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
//        //密码
//        webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
//        //重复密码
//        webDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("123456");
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
//        webDriver.findElement(By.cssSelector("#policy")).click();
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
//        webDriver.findElement(By.cssSelector("#submit")).click();
//    }

    /** 登录成功
     *  输入正确的用户名，密码 登录成功
     */
    @Order(2)
    @ParameterizedTest
    @CsvFileSource(resources = "login.csv")
     void loginSuccess(String username,String password,String index_url,String title) throws InterruptedException {
        webDriver.get("http://39.107.238.82:58080/sign-in.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
         sleep(3000);
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals(index_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.HOURS);
        String cur_title = webDriver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals(title,cur_title);

    }


    /**
     * 发布帖子
     */
    @Order(3)
    @Disabled
    @Test
    void publish() throws InterruptedException {

        webDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        WebElement element = webDriver.findElement(By.cssSelector("#article_post_borad"));
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        Select select = new Select(element);
        //下拉框选择Spring
        select.selectByIndex(4);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#article_post_title")).sendKeys("【Spring】之基础概念和使用");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        sleep(3000);

        webDriver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")).sendKeys("1111111");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        sleep(3000);
        //发布
        webDriver.findElement(By.cssSelector("#article_post_submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

    }
    /**
     *  查看帖子
     */
    @Order(4)
    @Test
    void show() throws InterruptedException {
        sleep(1000);
        webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        sleep(3000);
        String cur_title = webDriver.findElement(By.xpath("//*[@id=\"details_article_content_title\"]")).getText();
        Assertions.assertEquals("测试发布帖子",cur_title);

    }

    /**
     * 点赞帖子
     */
    @Order(5)
    @Test
    void like() throws InterruptedException {
        //找到点赞按钮
        sleep(3000);
        webDriver.findElement(By.cssSelector("#details_btn_like_count")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        sleep(3000);

        //点赞数加1
        String like_count = webDriver.findElement(By.cssSelector("#details_article_likeCount")).getText();
        Assertions.assertEquals("16",like_count);

    }

    /**
     * 删除帖子
     */
    @Order(6)
    @Disabled
    @Test
    void delete() throws InterruptedException {
        sleep(3000);
        //找到删除按钮
        webDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[2]/div/div/div[1]/div[2]/div[2]/div/div[3]/div/a")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        sleep(3000);
        String title = webDriver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("首页",title);
    }

//    /**
//     * 个人中心 修改昵称
//     */
//    @Order(7)
//    @Test
//     void center() {
//         webDriver.findElement(By.cssSelector("#index_nav_nickname")).click();
//         webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//         webDriver.findElement(By.cssSelector("#index_user_settings")).click();
//         webDriver.findElement(By.cssSelector("#setting_input_nickname")).sendKeys("九九九");
//         webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//         webDriver.findElement(By.cssSelector("#setting_submit_nickname")).click();
//         webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//         String nick_name = webDriver.findElement(By.cssSelector("#settings_nickname")).getText();
//         Assertions.assertEquals("九九九",nick_name);
//     }
//
//    /**
//     * 修改邮箱地址
//     */
//    @Test
//    void address() {
//
//        webDriver.findElement(By.cssSelector("#setting_input_email")).sendKeys("123456@qq。com");
//        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        //提交修改
//        webDriver.findElement(By.cssSelector("#setting_submit_email")).click();
//        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        String address = webDriver.findElement(By.cssSelector("#setting_input_email")).getText();
//        System.out.println(address);
//    }

}
